<?php defined('SYSPATH') OR die('No direct access allowed.');

class Pagetemplate_Model extends ORM{
	
	 protected $sorting = array('title' => 'asc');


	
	public function get_container_array(){
		if($this->container_param==''){
			return false;
		}
		return json_decode($this->container_param);
		//return explode(';', $this->container_param);
	}
	
	public function get_container_title_array(){
		if($this->container_param=='' ){
			return false;
		}
		$jsonarray= json_decode($this->container_param);
		
		$retarray=array();
		
	
		if(is_array($jsonarray)){
		
			foreach($jsonarray as $container){
				if($container->title!=''){
					$retarray[]=$container->title;
				}
			}
		}
		return $retarray;
	}
	
	
	
	public function get_container_modules_array($container_title){
		if($this->container_param=='' || $container_title==''){
			return false;
		}
		
		$retarray=array();
		$containers=json_decode($this->container_param);
		$mymodules=NULL;
		
		foreach($containers as $container){
			
			if($container->title==$container_title){
				$mymodules=$container->mymodules;
				
			}
		}
		if($mymodules==NULL){
			return false;
		}
		
		foreach($mymodules as $mymodule=>$onoff){
			if($onoff==1){		
				$retarray[]=ORM::factory('contentmodule', $mymodule);
			}
		}
		
		return $retarray;
	}
	
	
	
	public function get_active_contentmodules(){
		$contmodarray=array();
		$contmodidarray=array();
		
		if($this->container_param==''){
			return NULL;
		}
		
		$contarray=json_decode($this->container_param);
		foreach($contarray as $container){
			if(isset($container->mymodules)){
					 
				foreach($container->mymodules as $key=>$value){
					if($value=='1'){
						
						$contmodidarray[]=$key;				
					
					}	
				}
			}
		}
		
		array_unique($contmodidarray);
		foreach($contmodidarray as $contmodid){
			$contmodarray[]=ORM::factory('contentmodule', $contmodid);
		}
		return $contmodarray;
		
	}
}

